﻿using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Forums;
using Nop.Plugin.Widgets.ForumLast.Models;
using Nop.Services.Configuration;
using Nop.Services.Forums;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.Media;
using Nop.Services.Seo;
using Nop.Services.Stores;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Models.Boards;
using System;
using System.Web.Mvc;
using Nop.Services.Customers;

namespace Nop.Plugin.Widgets.ForumLast.Controllers
{
    public class WidgetsForumLastController : BasePluginController
    {
        private readonly IWorkContext _workContext;
        private readonly IStoreContext _storeContext;
        private readonly IStoreService _storeService;
        private readonly IPictureService _pictureService;
        private readonly ISettingService _settingService;
        private readonly ICacheManager _cacheManager;
        private readonly ILocalizationService _localizationService;

        private readonly CustomerSettings _customerSettings;
        private readonly ForumSettings _forumSettings;
        private readonly IForumService _forumService;
        private readonly IDateTimeHelper _dateTimeHelper;

        public WidgetsForumLastController(IWorkContext workContext,
            IStoreContext storeContext,
            IStoreService storeService, 
            IPictureService pictureService,
            ISettingService settingService,
            ICacheManager cacheManager,
            ILocalizationService localizationService,
            CustomerSettings customerSettings,
            IForumService forumService,
            ForumSettings forumSettings,
            IDateTimeHelper dateTimeHelper)
        {
            this._workContext = workContext;
            this._storeContext = storeContext;
            this._storeService = storeService;
            this._pictureService = pictureService;
            this._settingService = settingService;
            this._cacheManager = cacheManager;
            this._localizationService = localizationService;
            this._customerSettings = customerSettings;
            this._forumService = forumService;
            this._forumSettings = forumSettings;
            this._dateTimeHelper = dateTimeHelper;
        }


        #region Utilities

        [NonAction]
        protected virtual ForumTopicRowModel PrepareForumTopicRowModel(ForumTopic topic)
        {
            var topicModel = new ForumTopicRowModel()
            {
                Id = topic.Id,
                Subject = topic.Subject,
                SeName = topic.GetSeName(),
                LastPostId = topic.LastPostId,
                NumPosts = topic.NumPosts,
                Views = topic.Views,
                NumReplies = topic.NumReplies,
                ForumTopicType = topic.ForumTopicType,
                CustomerId = topic.CustomerId,
                AllowViewingProfiles = _customerSettings.AllowViewingProfiles,
                CustomerName = topic.Customer.FormatUserName(),
            };

            var forumPosts = _forumService.GetAllPosts(topic.Id, 0, string.Empty, 1, _forumSettings.PostsPageSize);
            topicModel.TotalPostPages = forumPosts.TotalPages;

            return topicModel;
        }

        #endregion


        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure()
        {
            //load settings for a chosen store scope
            var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var forumLastSettings = _settingService.LoadSetting<ForumLastSettings>(storeScope);
            var model = new ConfigurationModel();
            model.WidgetsZoneId = Convert.ToInt32(forumLastSettings.ZonesOptions);
            model.WidgetsZoneValues = forumLastSettings.ZonesOptions.ToSelectList();
            model.Publish = forumLastSettings.Publish;
            model.DisplayOrder = forumLastSettings.DisplayOrder;
            model.DirectionId = Convert.ToInt32(forumLastSettings.DirectionOptions);
            model.DirectionValues = forumLastSettings.DirectionOptions.ToSelectList();
            model.Visible = forumLastSettings.Visible;
            model.Interval = forumLastSettings.Interval;
            model.ShowNavigation = forumLastSettings.ShowNavigation;

            return View("~/Plugins/Widgets.ForumLast/Views/Configure.cshtml", model);
        }

        [HttpPost]
        [AdminAuthorize]
        [ChildActionOnly]
        public ActionResult Configure(ConfigurationModel model)
        {
            var forumLastSettings = _settingService.LoadSetting<ForumLastSettings>();
            forumLastSettings.ZonesOptions = (BlockZones) model.WidgetsZoneId;
            forumLastSettings.Publish = model.Publish;
            forumLastSettings.DisplayOrder = model.DisplayOrder;
           
            forumLastSettings.DirectionOptions =(ForumLastDirection) model.DirectionId;
            forumLastSettings.Visible = model.Visible;
            forumLastSettings.Interval = model.Interval;
            forumLastSettings.ShowNavigation = model.ShowNavigation;
          

            _settingService.SaveSetting(forumLastSettings);
            SuccessNotification(_localizationService.GetResource("Admin.Plugins.Saved"));
            return Configure();
        }

        [ChildActionOnly]
        public ActionResult PublicInfo(string widgetZone, object additionalData = null)
        {
            var forumLastSettings = _settingService.LoadSetting<ForumLastSettings>(_storeContext.CurrentStore.Id);
            if (!_forumSettings.ForumsEnabled || !forumLastSettings.Publish)
            {
                return Content("");
            }

			var topics = _forumService.GetActiveTopics(0, 0, _forumSettings.HomePageActiveDiscussionsTopicCount);
			if (topics.Count == 0)
                return Content("");

            var model = new ForumLastActiveDiscussionsModel();
            foreach (var topic in topics)
            {
                var topicModel = PrepareForumTopicRowModel(topic);
                model.ForumTopics.Add(topicModel);
            }
            model.ViewAllLinkEnabled = true;
            model.ActiveDiscussionsFeedEnabled = _forumSettings.ActiveDiscussionsFeedEnabled;
            model.PostsPageSize = _forumSettings.PostsPageSize;
           
            //script
            model.Direction = forumLastSettings.DirectionOptions.ToString();
            model.Interval = forumLastSettings.Interval;
            model.ShowNavigation = forumLastSettings.ShowNavigation;
            model.Visible = forumLastSettings.Visible;

            return View("~/Plugins/Widgets.ForumLast/Views/PublicInfo.cshtml", model);
        }
    }
}